<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-row gutter="24">
        <el-form-item label="分省编码" prop="orgCode">
          <el-select v-model="queryParams.orgCode" placeholder="请输入分省编码" filterable clearable
            @keyup.enter.native="handleQuery">
            <el-option v-for="orgCode in orgCodeList" :key="orgCode.id" :label="orgCode.name + '-' + orgCode.code"
              :value="orgCode.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="专辑ID" prop="albumId">
          <el-input v-model="queryParams.albumId" placeholder="请输入专辑ID" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="专辑名称" prop="albumName">
          <el-input v-model="queryParams.albumName" placeholder="请输入专辑ID" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="剧集ID" prop="episodeId">
          <el-input v-model="queryParams.episodeId" placeholder="请输入剧集ID" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="剧集名称" prop="episodeName">
          <el-input v-model="queryParams.episodeName" placeholder="请输入剧集名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="专辑ID集合" prop="albumIds">
          <el-input v-model="queryParams.albumIds" placeholder="请输入剧集IDs" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="剧集ID集合" prop="episodeIds">
          <el-input v-model="queryParams.episodeIds" placeholder="请输入剧集IDs" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="CP信息" prop="cpCode">
          <el-input v-model="queryParams.cpCode" placeholder="请输入cp编码" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="剧集CP状态">
          <el-select v-model="queryParams.episodeCpStatus" placeholder="请选择CP状态" clearable
            @keyup.enter.native="handleQuery">
            <el-option label="未授权" value='0'></el-option>
            <el-option label="已授权" value='1'></el-option>
            <el-option label="排播" value='3'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注入状态">
          <el-select v-model="queryParams.resourceStreamInjectStatus" placeholder="请选择注入状态" clearable
            @keyup.enter.native="handleQuery">
            <el-option label="待注入" value='0'></el-option>
            <el-option label="注入中" value='1'></el-option>
            <el-option label="已注入" value='2'></el-option>
            <el-option label="注入失败" value='3'></el-option>
            <el-option label="已删除" value='6'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注入状态(org)">
          <el-select v-model="queryParams.resourceStreamOrgInjectStatus" placeholder="请选择注入状态" clearable
            @keyup.enter.native="handleQuery">
            <el-option label="待注入" value='0'></el-option>
            <el-option label="注入中" value='1'></el-option>
            <el-option label="已注入" value='2'></el-option>
            <el-option label="注入失败" value='3'></el-option>
            <el-option label="已删除" value='6'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
          @click="handleProvinceVideoTriggers">批量注入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleModifyProvinceResourceStreamOrgs"
          v-hasPermi="['jishiting:rms:resourcestream:province:modifyProvinceResourceStreamOrg']">修改分省注入状态</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-plus" size="mini" @click="handleDeleteOrgDatas">批量删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resourceStreamOrgData" @selection-change="handleSelectionChange"
      max-height="960">
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column label="省网编码" align="center" prop="orgCode" width="100" />
      <el-table-column label="专辑ID" align="center" prop="albumId" width="180" />
      <el-table-column label="专辑名称" align="center" prop="albumName" width="180" />
      <el-table-column label="专辑分类" align="center" prop="categoryName" width="180" />
      <el-table-column label="剧集ID" align="center" prop="episodeId" width="180" />
      <el-table-column label="剧集名称" align="center" prop="episodeName" width="180" />
      <el-table-column label="剧集排序" align="center" prop="albumOrder" width="80" />
      <el-table-column label="码流品质" align="center" prop="quality" width="100" />
      <el-table-column label="原剧集ID" align="center" prop="sourceId" width="180" />
      <el-table-column label="源站CDN状态" align="center" prop="originInjectStatus" width="80" fixed="right" />
      <el-table-column label="分省CDN状态" align="center" prop="nodeInjectStatus" width="80" fixed="right" />
      <el-table-column label="RMSCP编码" align="center" prop="cpCode" width="180" />
      <el-table-column label="分省CP编码" align="center" prop="nodeCpCode" width="180" />
      <el-table-column label="专辑状态" align="center" prop="albumStatus" width="80" fixed="right" />
      <el-table-column label="专辑CP状态" align="center" prop="albumCpStatus" width="80" fixed="right" />
      <el-table-column label="剧集状态" align="center" prop="episodeStatus" width="80" fixed="right" />
      <el-table-column label="剧集CP状态" align="center" prop="episodeCpStatus" width="80" fixed="right" />
      <el-table-column label="注入状态(res_base)" align="center" prop="episodeInjectStatus" width="80" fixed="right" />
      <el-table-column label="码流状态" align="center" prop="status" width="80" fixed="right" />
      <el-table-column label="码流注入状态" align="center" prop="resourceStreamInjectStatus" width="80" fixed="right" />
      <el-table-column label="码流注入状态(org)" align="center" prop="resourceStreamOrgInjectStatus" width="120"
        fixed="right" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button @click="handleProvinceVideoTrigger(scope.row)"
            v-hasPermi="['jishiting:rms:resourcestream:province:videotrigger']" type="text"
            size="small">分省注入</el-button>
          <el-button @click="handleModifyProvinceResourceStreamOrg(scope.row)"
            v-hasPermi="['jishiting:rms:resourcestream:province:modifyProvinceResourceStreamOrg']" type="text"
            size="small">修改分省注入状态</el-button>
          <el-button @click="handleDeleteOrgData(scope.row)"
            v-hasPermi="['jishiting:rms:resourcestream:province:videodelete']" type="text"
            size="small">分省数据删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      :page-sizes="[10, 20, 30, 50, 100, 200, 300, 400, 500]" @pagination="getList" />
  </div>
</template>

<script>
import { selectProvinceWithEpisodeListPage } from "@/api/jishiting/rms";
import { provinceVideoTrigger, modifyProvinceResourceStreamOrg } from "@/api/jishiting/datainjection";
import { deleteResourceStreamOrgData, selectEnableOrgCodeList } from "@/api/jishiting/gscms.js";
import { sleep } from "@/utils/common.js"
export default {
  name: "Notice",
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      episodes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //抽屉
      drawer: false,
      resourceStreamOrgData: [],
      table: false,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        albumId: undefined,
        episodeId: undefined,
        episodeIds: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 分省编码列表
      orgCodeList: [],
    };
  },
  created() {
    this.selectEnableOrgCodeList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      selectProvinceWithEpisodeListPage(this.queryParams)
        .then((response) => {
          this.resourceStreamOrgData = response.data;
          this.total = response.total;
          this.loading = false;
        })
        .catch((error) => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.episodes = selection.map(item => item);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    selectEnableOrgCodeList() {
      selectEnableOrgCodeList().then((response) => {
        this.orgCodeList = response.data;
      })
    },
    handleProvinceVideoTrigger(selection) {
      this.$confirm("确认分省注入吗？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        provinceVideoTrigger({
          orgCode: selection.orgCode,
          reinject: true,
          videoId: selection.episodeId,
        }).then((response) => {
          this.$message({
            message: "注入成功",
            type: "success",
          });
          this.getList();
        });
      })
    },
    handleProvinceVideoTriggers() {
      this.$confirm("确认批量分省注入吗？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.episodes.forEach(item => {
          provinceVideoTrigger({
            orgCode: item.orgCode,
            reinject: true,
            videoId: item.episodeId,
          })
          this.getList();
        })
      })
    },


    handleModifyProvinceResourceStreamOrgs() {
      this.$confirm("确认批量修改分省注入状态吗？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.episodes.forEach(item => {
          modifyProvinceResourceStreamOrg({
            orgCode: item.orgCode,
            albumId: item.albumId,
            videoId: item.episodeId,
            injectStatus: 0
          })
        })
        this.$message({
          message: "修改状态成功",
          type: "success",
        });
        this.handleRefreshResourceStreamOrgData()
      })
    },

    handleModifyProvinceResourceStreamOrg(selection) {
      this.$confirm("确认修改分省注入状态吗？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        modifyProvinceResourceStreamOrg({
          orgCode: selection.orgCode,
          albumId: selection.albumId,
          videoId: selection.episodeId,
          injectStatus: 0
        }).then((response) => {
          this.$message({
            message: "修改状态成功",
            type: "success",
          });
          this.handleRefreshResourceStreamOrgData()
        });
      })
    },

    handleDeleteOrgDatas() {
      this.$confirm("确认批量分省删除吗？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.episodes.forEach(item => {
          deleteResourceStreamOrgData({
            orgCode: item.orgCode,
            albumId: item.albumId,
            videoId: item.episodeId,
            resType: item.resType,
          })
          sleep(1000)
        })
        this.handleQuery();
      })
    },
    handleDeleteOrgData(row) {
      this.$confirm("确认分省删除吗？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteResourceStreamOrgData({
          orgCode: row.orgCode,
          albumId: row.albumId,
          videoId: row.episodeId,
          resType: row.resType,
        }).then((response) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
        });
        this.handleQuery();
      })
    }
  }
};
</script>
